package com.lixingqu.mvpdemo.ui.itemdrag;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;

import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemDragListener;
import com.lixingqu.mvpdemo.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.bumptech.glide.gifdecoder.GifHeaderParser.TAG;

public class DragActivity extends Activity {

    @BindView(R.id.left_recyclerview)
    RecyclerView leftRecyclerview;
    @BindView(R.id.right_rectclerview)
    RecyclerView rightRectclerview;
    DragAdapter adapter;
    DragAdapter rightadapter;
    List<String> list;
    private ItemTouchHelper leftmItemTouchHelper;
    private MyCallback leftmItemDragAndSwipeCallback;
    private ItemTouchHelper rightmItemTouchHelper;
    private MyCallback rightmItemDragAndSwipeCallback;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drag);
        ButterKnife.bind(this);
        getData();
        rightadapter = new DragAdapter(list);
        adapter = new DragAdapter(list);
        leftRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        leftRecyclerview.setAdapter(adapter);
        rightRectclerview.setLayoutManager(new LinearLayoutManager(this));
        rightRectclerview.setAdapter(rightadapter);
        OnItemDragListener leftlistener = new OnItemDragListener() {
            @Override
            public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) {
                Log.d("lxq", "drag start");
                BaseViewHolder holder = ((BaseViewHolder)viewHolder);
                holder.setTextColor(R.id.tv_item_drag, Color.GREEN);
            }

            @Override
            public void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.ViewHolder target, int to) {
                Log.d("lxq", "move from: " + source.getAdapterPosition() + " to: " + target.getAdapterPosition());
            }

            @Override
            public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {
                Log.d(TAG, "drag end");
                BaseViewHolder holder = ((BaseViewHolder)viewHolder);
                holder.setTextColor(R.id.tv_item_drag, Color.BLACK);

            }
        };
        OnItemDragListener rightlistener = new OnItemDragListener() {
            @Override
            public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) {
                Log.d("lxq", "drag start");
                BaseViewHolder holder = ((BaseViewHolder)viewHolder);
                holder.setTextColor(R.id.tv_item_drag, Color.GREEN);
            }

            @Override
            public void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.ViewHolder target, int to) {
                Log.d("lxq", "move from: " + source.getAdapterPosition() + " to: " + target.getAdapterPosition());
            }

            @Override
            public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {
                Log.d(TAG, "drag end");
                BaseViewHolder holder = ((BaseViewHolder)viewHolder);
                holder.setTextColor(R.id.tv_item_drag, Color.BLACK);

            }
        };
        leftmItemDragAndSwipeCallback = new MyCallback(adapter);
        leftmItemTouchHelper = new ItemTouchHelper(leftmItemDragAndSwipeCallback);
        leftmItemTouchHelper.attachToRecyclerView(leftRecyclerview);
        // 开启拖拽
        adapter.enableDragItem(leftmItemTouchHelper);
        adapter.setOnItemDragListener(leftlistener);

        rightmItemDragAndSwipeCallback = new MyCallback(rightadapter);
        rightmItemTouchHelper = new ItemTouchHelper(rightmItemDragAndSwipeCallback);
        rightmItemTouchHelper.attachToRecyclerView(rightRectclerview);
        // 开启拖拽
        rightadapter.enableDragItem(rightmItemTouchHelper);
        rightadapter.setOnItemDragListener(leftlistener);

    }

    public void getData() {
        list = new ArrayList<>();
        for (int i = 1; i <= 50; i++) {
            list.add("item "+i);
        }
    }
}
